//
//  PreferenceController.m
//  PicUploader
//
//  Created by Chen Zhang on 8/19/08.
//  Copyright 2008 overboming. All rights reserved.
//

#import "PreferenceController.h"
#import "PUBoard.h"
NSString * const ZCFavUserNameKey = @"FavUserName";
NSString * const ZCFavPasswordKey = @"FavPassword";
NSString * const ZCFavBoardKey = @"FavBoardName";
NSString * const ZCRemeberMeKey = @"FavRememberMe";
NSString * const ZCCookieStringKey = @"LastCookieString";
NSString * const ZCMaxThreadNumKey = @"MaxThreadNum";
NSString * const ZCBoardNameList = @"BoardNameList";
NSString * const ZCImageQualityKey = @"ImageQuality";
NSString * const ZCImageSizeKey = @"ImageSize";
NSString * const ZCImageLogArrayKey = @"ImageLogArray";

//notification
NSString * const ZCThreadNumChangedNotification = @"ThreadNumChanged";
NSString * const ZCBoardListChangedNotification = @"BoardListChanged";
NSString * const ZCFavBoardChangedNotification = @"FavBoardChanged";
NSString * const ZCImageQualityChangedNotification = @"ImageQualityChanged";
NSString * const ZCImageSizeChangedNotification = @"ImageSizeChanged";


@implementation PreferenceController
- (void)setupToolbar
{
    [self addView:generalPreferenceView label:@"General"];
    [self addView:accountPreferenceView label:@"Account"];
    [self addView:updatePreferenceView label:@"Update"];
    
    // Optional settings.
	[self setCrossFade:[[NSUserDefaults standardUserDefaults] boolForKey:@"fade"]];
	[self setShiftSlowsAnimation:YES];
    
}

+ (void)initialize
{
    // Create a dictionary
    NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
    
    NSMutableArray *array =  [[NSMutableArray alloc] init];
    [array addObject:[[PUBoard alloc] initWithBoardName:@"Mac"]];
    
	NSMutableArray *logArray = [[NSMutableArray alloc] init];
    // Archieve the strings
    NSData *usernameAsData = [NSKeyedArchiver archivedDataWithRootObject:@""];
    NSData *passwordAsData = [NSKeyedArchiver archivedDataWithRootObject:@""];
    NSData *boardNameAsData = [NSKeyedArchiver archivedDataWithRootObject:@"Mac"];
    NSData *cookieStringAsData = [NSKeyedArchiver archivedDataWithRootObject:@""];
    NSData *boardNameListAsData = [NSKeyedArchiver archivedDataWithRootObject:array];
    NSData *ImageQualityAsData = [NSKeyedArchiver archivedDataWithRootObject:@"Poor"];
    NSData *ImageSizeAsData = [NSKeyedArchiver archivedDataWithRootObject:@"Original"];
	NSData *ImageLogArrayAsData = [NSKeyedArchiver archivedDataWithRootObject:logArray];
    
    //put the defaults
    [defaultValues setObject:usernameAsData forKey:ZCFavUserNameKey];
    [defaultValues setObject:passwordAsData forKey:ZCFavPasswordKey];
    [defaultValues setObject:boardNameAsData forKey:ZCFavBoardKey];
    [defaultValues setObject:cookieStringAsData forKey:ZCCookieStringKey];
    [defaultValues setObject:[NSNumber numberWithBool:YES] forKey:ZCRemeberMeKey];  //default remember me
    [defaultValues setObject:[NSNumber numberWithInt:2] forKey:ZCMaxThreadNumKey];
    [defaultValues setObject:ImageQualityAsData forKey:ZCImageQualityKey];
    [defaultValues setObject:boardNameListAsData forKey:ZCBoardNameList];
    [defaultValues setObject:ImageSizeAsData forKey:ZCImageSizeKey];
	[defaultValues setObject:ImageLogArrayAsData forKey:ZCImageLogArrayKey];
    
    // Register the dictionary of defaults
    [[NSUserDefaults standardUserDefaults]
     registerDefaults: defaultValues];
    //NSLog(@"registered defaults: %@", defaultValues);
    
}



+ (NSString *)nibName
// Subclasses can override this to use a nib with a different name.
{
    return @"Preference";
}
//
- (NSString *)favUserName
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *usernameAsData = [defaults objectForKey:ZCFavUserNameKey];
    return [NSKeyedUnarchiver unarchiveObjectWithData:usernameAsData];
//    return @"keynote";
}
- (NSString *)favPassword
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *passwordAsData = [defaults objectForKey:ZCFavPasswordKey];
    return [NSKeyedUnarchiver unarchiveObjectWithData:passwordAsData];
}
- (NSString *)favBoard
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *boardNameAsData = [defaults objectForKey:ZCFavBoardKey];
    return [NSKeyedUnarchiver unarchiveObjectWithData:boardNameAsData];
}
- (BOOL)rememberMe
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:ZCRemeberMeKey];
}
- (NSInteger)maxThread
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults integerForKey:ZCMaxThreadNumKey];   
}

- (NSString *)imageQuality
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *imageQualityAsData = [defaults objectForKey:ZCImageQualityKey];
    return [NSKeyedUnarchiver unarchiveObjectWithData:imageQualityAsData];
}

- (NSString *)imageSize
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *imageSizeAsData= [defaults objectForKey:ZCImageSizeKey];
    return [NSKeyedUnarchiver unarchiveObjectWithData:imageSizeAsData];
}
- (NSMutableArray *)imageLogArray
{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSData *imageLogArrayAsData = [defaults objectForKey:ZCImageLogArrayKey];
	return [NSKeyedUnarchiver unarchiveObjectWithData:imageLogArrayAsData];
}

- (void)changeImageLogArray:(NSMutableArray *)array
{
	NSData *arrayAsData = [NSKeyedArchiver archivedDataWithRootObject:array];
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:arrayAsData forKey:ZCImageLogArrayKey];
	//NSLog(@"Image Log: %@", array); 
}

- (IBAction)changeFavUserName:(id)sender
{
    if([rememberMeCheckBox state]){
        //NSLog(@"change default user to someone else");
        NSString *username = [usernameField stringValue];
        if([username isEqualToString:@""])
            return;
        NSData *usernameAsData =
        [NSKeyedArchiver archivedDataWithRootObject:username];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:usernameAsData forKey:ZCFavUserNameKey];
    }
    [usernameField setStringValue:[self favUserName]];
}
- (IBAction)changeFavPassword:(id)sender
{
    if([rememberMeCheckBox state]){
        NSString *password = [passwordField stringValue];
        NSData *passwordAsData =
        [NSKeyedArchiver archivedDataWithRootObject:password];
        if([password isEqualToString:@""])
            return;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:passwordAsData forKey:ZCFavPasswordKey];        
    }
    [passwordField setStringValue:[self favPassword]];
}

- (IBAction)updateAccountInfo:(id)sender
{
    [self changeFavPassword:sender];
    [self changeFavUserName:sender];
}

- (IBAction)changeFavBoard:(id)sender
{
    NSString *boardname = [boardBox stringValue];
    if([boardname isEqualToString:@""])
        return;
    NSData *boardnameAsData =
    [NSKeyedArchiver archivedDataWithRootObject:boardname];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:boardnameAsData forKey:ZCFavBoardKey];
    
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc postNotificationName:ZCFavBoardChangedNotification object:self];
}
- (IBAction)changeRememberMe:(id)sender
{
    BOOL rememberMe = [rememberMeCheckBox state];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:rememberMe forKey:ZCRemeberMeKey];
}

- (IBAction)changeMaxThread:(id)sender
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    if([[maxThreadBox stringValue] isEqualToString:@""])
        return;
    NSNumber *it = [formatter numberFromString:[maxThreadBox stringValue]]; 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:[it integerValue] forKey:ZCMaxThreadNumKey];
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc postNotificationName:ZCThreadNumChangedNotification object:self];

}

- (IBAction)changeImageQuality:(id)sender
{
    NSString *imageQuality = [imageQualityBox stringValue];
    if([imageQuality isEqualToString:@""])
        return;
    NSData *imageQualityAsData =
    [NSKeyedArchiver archivedDataWithRootObject:imageQuality];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:imageQualityAsData forKey:ZCImageQualityKey];
    
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc postNotificationName:ZCImageQualityChangedNotification object:self];
}

- (IBAction)changeImageSize:(id)sender
{
    NSString *imageSize = [imageSizeBox stringValue];
    if([imageSize isEqualToString:@""])
        return;
    NSData *imageSizeAsData =
    [NSKeyedArchiver archivedDataWithRootObject:imageSize];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:imageSizeAsData forKey:ZCImageSizeKey];
    
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc postNotificationName:ZCImageSizeChangedNotification object:self];
}


- (void)showProjectHome:(id)sender
{
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://code.google.com/p/attachbot"]];
}

- (void)windowDidLoad
{
      [super windowDidLoad];
      [usernameField setStringValue:[self favUserName]];
      [passwordField setStringValue:[self favPassword]];
      [boardBox setStringValue:[self favBoard]];
      [rememberMeCheckBox setState:[self rememberMe]];
      [maxThreadBox setStringValue:[NSString stringWithFormat:@"%d",[self maxThread]]];
      [imageQualityBox setStringValue:[self imageQuality]];
      [imageSizeBox setStringValue:[self imageSize]];
}


@end
